//
// GraphFactory.cpp for src in /home/titouan/Dropbox/Code/nibbler-epitech-2017/src
//
// Made by titouan creach
// Login   <creach_a@epitech.net>
//
// Started on  mer. avril 02 13:19:27 2014 titouan creach
// Last update mer. avril 02 13:19:27 2014 titouan creach
//

#include <dlfcn.h>
#include <iostream>
#include "exceptions.hpp"
#include "GraphFactory.hpp"

void    *GraphFactory::hlib = 0;

IGraph *GraphFactory::getGraphics(const std::string &path, int argc, char **argv,
                                  int w, int h)
{
  hlib = dlopen(path.c_str(), RTLD_LAZY | RTLD_NOW | RTLD_DEEPBIND);
  if (!hlib)
    {
      throw LoadGraphicLibrairie(dlerror(), path);
    }
  f_constructor f = (f_constructor)dlsym(hlib, "create");
  if (f == 0)
    throw LoadFunctionException("create", path);
  return f(argc, argv, w, h);
}

void GraphFactory::deleteGraphcis(IGraph *g)
{
  f_destruction f = (f_destruction)dlsym(hlib, "destroy");
  if (f == 0)
    throw LoadFunctionException("destroy", "the librairy");
  f(g);
}
